package com.me.chat.util.img;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Author qiu
 * @create 2020/3/13 17:51
 * @Des image工具类
 */
public class ImageUtil {

    //网络图片转换为byte数组
    public static byte[] imageToByte(String imagePath) {
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        byte[] buffer = null;
        try {
            // 创建URL
            URL url = new URL(imagePath);
            // 创建链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            inputStream = conn.getInputStream();
            outputStream = new ByteArrayOutputStream();
            // 将内容读取内存中
            buffer = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            buffer = outputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    // 关闭inputStream流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    // 关闭outputStream流
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return buffer;
    }

    //获得高
    public static Integer getImageHeight(MultipartFile multipartFile) throws IOException {

        String fileName = multipartFile.getOriginalFilename();
        String prefix=fileName.substring(fileName.lastIndexOf("."));
        // 创建临时文件
        File file = File.createTempFile(prefix, String.valueOf(System.currentTimeMillis()));
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
        BufferedImage bufferedImage = ImageIO.read(file); // 通过临时文件获取图片流
        if (bufferedImage == null) {
            // 证明上传的文件不是图片，获取图片流失败，不进行下面的操作
            return 0;
        }

        Integer height = bufferedImage.getHeight(); // 通过图片流获取图片高度
        // 省略逻辑判断

        return height;
    }

    //获得宽
    public static Integer getImageWidth(MultipartFile multipartFile) throws IOException {

        String fileName = multipartFile.getOriginalFilename();
        String prefix=fileName.substring(fileName.lastIndexOf("."));
        // 创建临时文件
        File file = File.createTempFile(prefix, String.valueOf(System.currentTimeMillis()));
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
        BufferedImage bufferedImage = ImageIO.read(file); // 通过临时文件获取图片流
        if (bufferedImage == null) {
            return 0;
        }
        Integer width = bufferedImage.getWidth(); // 通过图片流获取图片宽度

        return width;
    }
}
